<?php
global $_GPC, $_W;
load()->func('tpl');
//var_dump($_W);die;
$GLOBALS['frames'] = $this->getMainMenu2();
$storeid=$_COOKIE["storeid"];
$cur_store = $this->getStoreById($storeid);
//var_dump($storeid);die;
$type = $_GPC['type']?$_GPC['type']:0;
$where=" where a.uniacid=:uniacid and a.del = 0 and a.store_id = :storeid";
$data[':uniacid']=$_W['uniacid'];
$data[':storeid']=$storeid;
if(!empty($type)){
    $where .= " and a.status = :status";
    $data[':status']=$type;
}
if(!empty($_GPC['keywords'])){
    $where.=" and (a.yd_name LIKE  concat('%', :name,'%') || a.order_sn LIKE  concat('%', :name,'%'))";
    $data[':name']=$_GPC['keywords'];
}
if($_GPC['time']){
    $start=strtotime($_GPC['time']['start']);
    $end=strtotime($_GPC['time']['end']);
    $where.=" and UNIX_TIMESTAMP(a.yd_time) >={$start} and UNIX_TIMESTAMP(a.yd_time)<={$end}";
}
if($_GPC['table_id']){
    $where .= " and a.table_id = :table_id";
    $data[':table_id'] = $_GPC['table_id'];
}
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$sql="SELECT a.*,f.name as f_name FROM ".tablename('cjdc_ydorder'). " a" .' left join '.tablename('cjdc_table_type').' f on f.id = a.tabletype_id'.$where." ORDER BY a.id DESC";
$sql_1="SELECT count(a.id) FROM ".tablename('cjdc_ydorder'). " a"  . " left join " . tablename("cjdc_store") . " b on a.store_id=b.id " .'left join '.tablename('cjdc_table').' e on e.id = a.table_id  '.$where." ORDER BY a.id DESC";
$total=pdo_fetchcolumn($sql_1,$data);
$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$list=pdo_fetchall($select_sql,$data);
//pdo_debug(true);
//var_dump($list);die;
$pager = pagination($total, $pageindex, $pagesize);
if($_GPC['op']=='ok'){
    pdo_delete('cjdc_formid',array('time <='=>time()-60*60*24*7));
    $data2['yy_state']=3;
    // $data2['completion_time']=time();
    $rst=pdo_update('cjdc_order',$data2,array('id'=>$_GPC['id']));
    if($rst){
        file_get_contents("".$_W['siteroot']."app/index.php?i=".$_W['uniacid']."&c=entry&a=wxapp&do=addintegral&m=zh_cjdianc&type=4&order_id=".$_GPC['id']);
        //有效分销佣金
        $this->updcommission($_GPC['id']);
        ///////////////模板消息通过///////////////////
        function getaccess_token($_W){
            $res=pdo_get('cjdc_system',array('uniacid'=>$_W['uniacid']));
            $appid=$res['appid'];
            $secret=$res['appsecret'];
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            $data = curl_exec($ch);
            curl_close($ch);
            $data = json_decode($data,true);
            return $data['access_token'];
        }
        //设置与发送模板信息
        function set_msg($_W){
            $access_token = getaccess_token($_W);
            $res=pdo_get('cjdc_message',array('uniacid'=>$_W['uniacid']));
            $res2=pdo_get('cjdc_order',array('id'=>$_GET['id']));
            $user=pdo_get('cjdc_user',array('id'=>$res2['user_id']));
            $store=pdo_get('cjdc_store',array('id'=>$res2['store_id']));
            $table=pdo_get('cjdc_table_type',array('id'=>$res2['table_id']));
            $form=pdo_get('cjdc_formid',array('user_id'=>$res2['user_id'],'time >='=>time()-60*60*24*7));
            $formwork ='{
           "touser": "'.$user["openid"].'",
           "template_id": "'.$res["yy_tid"].'",
           "page": "zh_cjdianc/pages/Liar/loginindex",
           "form_id":"'.$form['form_id'].'",
           "data": {
             "keyword1": {
               "value": "'.$store['name'].'",
               "color": "#173177"
             },
             "keyword2": {
               "value":"'.$res2['order_num'].'",
               "color": "#173177"
             },
             "keyword3": {
               "value": "'.$res2['tel'].'",
               "color": "#173177"
             },
             "keyword4": {
               "value":  "'.$res2['tableware'].'",
               "color": "#173177"
             },
             "keyword5": {
               "value": "'.$table['name'].'",
               "color": "#173177"
             },
             "keyword6": {
               "value": "'.$res2['time'].'",
               "color": "#173177"
             },
             "keyword7": {
               "value": "'.$res2['money'].'",
               "color": "#173177"
             },
              "keyword8": {
               "value": "'.$res2['delivery_time'].'",
               "color": "#173177"
             },
              "keyword9": {
               "value": "预约通过,请在规定时间前往就餐~",
               "color": "#173177"
             }
           }
         }';
            // $formwork=$data;
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
            // return $data;
            pdo_delete('cjdc_formid',array('id'=>$form['id']));
        }
        echo set_msg($_W);
        ///////////////模板消息///////////////////
        message('确认成功',$this->createWebUrl('ydorder',array()),'success');
    } else{
        message('确认失败','','error');
    }
}
if($_GPC['op']=='delete'){
    $res=pdo_delete('cjdc_ydorder',array('id'=>$_GPC['id']));
    if($res){
        message('删除成功！', $this->createWebUrl('newinydorder'), 'success');
    } else{
        message('删除失败！','','error');
    }
}

include $this->template('web/newinydorder');